﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Interfaces;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class Beam : VehicleElement
    {
        #region Properties

        /// <summary>Gets or sets the beam name</summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>Gets the id of the first node</summary>
        public string NodeId1
        {
            get;
            set;
        }

        /// <summary>Gets the id of the second node</summary>
        public string NodeId2
        {
            get;
            set;
        }

        #endregion

        #region CTORs

        public Beam()
        {

        }

        public Beam(string nodeId1, string nodeId2)
        {
            NodeId1 = nodeId1;
            NodeId2 = nodeId2;
        }

        #endregion

        #region Methods

        public override IVehicleElement FromJBeam(JToken token)
        {
            var arr = token as JArray;
            if (arr == null) return this;

            NodeId1 = token[0].FromJBeam<string>();
            NodeId2 = token[1].FromJBeam<string>();

            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray { NodeId1, NodeId2 };
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}", NodeId1, NodeId2);
        }

        public void GetNodes(Vehicle vehicle, out Node node1, out Node node2)
        {
            node1 = vehicle.FindNode(NodeId1);
            node2 = vehicle.FindNode(NodeId2);
        }

        #endregion
    }
}
